﻿using System;
using System.IO;
using System.Linq;
using Wicresoft.Logger;
using Wicresoft.Task.Common;
using Wicresoft.Task.Team.Entity;

namespace Wicresoft.Task.Team
{
    class Nofitier
    {
        TfsAutoNotifyContext tfsDB = new TfsAutoNotifyContext();
        private string website;
        private string CC;

        public Nofitier(string Website, string cc)
        {
            this.website = Website;
            this.CC = cc;
        }
        public EmailTemplate CreateEmail(ShelveInfo s)
        {
            EmailTemplate email = new EmailTemplate();
            using (TfsAutoNotifyContext TeamDB = new TfsAutoNotifyContext())
            {
                string tempBody = Helper.ReadFile("Body.html");
                var itemStr = tfsDB.WorkItemInfoes.Where(p => p.ShelvesetId == s.ShelvesetId).ToArray();
                string workItemInfor = Helper.ReturnWorkItemInfo(itemStr, TeamDB, s);
                string codeReviewer = s.CodeReviewer;
                string mailTo = QueryMailTo(codeReviewer, tfsDB);
                if (mailTo == "")
                {
                    email = null;
                    var emp = tfsDB.EmployeesInfoes.FirstOrDefault(p => s.OwnerName.Contains(p.Alias));
                    string errorEmail = emp.Email;
                    string name = emp.EnglishName;
                    string bod = Helper.ReadFile("InvalidCodeReview.html");
                    string body = string.Format(bod, name, "You Type A Invailid Code Reviewer:" + codeReviewer + "<Br/> You Should Delete The Invalid Shelve And Then Reshelve.Thanks!");
                    SmtpHelper.SendEmail("You Type A Invailid Code Reviewer:" + codeReviewer, body, "noreply@ws-fs-01.local", errorEmail);

                }
                else
                {
                    string codeReviewerEnglishName = TeamDB.EmployeesInfoes.First(p => p.Email == mailTo).EnglishName;
                    string body = string.Format(tempBody, codeReviewerEnglishName, s.Name, s.OwnerName.TrimStart(), codeReviewerEnglishName.TrimStart(), workItemInfor.TrimStart(), s.Comment.TrimStart(), website, s.ShelvesetId);
                    email.Bcc = "i-jibw@wicresoft.com";
                    email.Body = body;
                    Employee _Es = TeamDB.EmployeesInfoes.First(p => s.OwnerName.Contains(p.Alias));
                    email.Cc = CC + ";" + _Es.Email;
                    email.CreatedTime = DateTime.Now;
                    email.From = "noreply@ws-fs-01.local";
                    email.ReplyTo = _Es.Email;
                    email.Subject = "Code Review Request For  " + s.Name;
                    email.To = mailTo;
                }
            }
            return email;
        }
        public string QueryMailTo(string codeReviewer, TfsAutoNotifyContext TeamDB)
        {
            string email = "";

            var employeesInfo = TeamDB.EmployeesInfoes.ToList();
            foreach (var x in employeesInfo)
            {
                if (codeReviewer.Trim().Length != 0)
                {
                    if (IsWho(x, codeReviewer))
                    {
                        email = x.Email;
                    }
                }
            }
            return email;

        }
        public bool IsWho(Employee p, string codeReviewers)
        {
            bool x = false;

            string codeReviewer = codeReviewers.Trim();
            if (p.Name.Trim().Contains(codeReviewer))
            {
                x = true;
            }

            if (p.EnglishName.Trim().Equals(codeReviewer, StringComparison.OrdinalIgnoreCase))
            {
                x = true;
            }
            if (p.Pinyin.Trim().ToLower().Contains(codeReviewer.ToLower()))
            {
                x = true;
            }

            if (p.Alias.Trim().ToUpper().Contains(codeReviewer.ToUpper()))
            {
                x = true;

            }
            return x;
        }
    }
}
